package com.base.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.base.entity.bo.OrganizationBo;
import com.base.entity.bo.SciencePersonBo;
import com.base.entity.vo.OrganizationVo;
import com.base.entity.vo.SciencePersonVo;
import com.base.mapper.OrganizationMapper;
import com.base.mapper.SciencePersonMapper;
import com.base.utils.CutPageBean;
import com.common.core.domain.entity.SysDept;
import com.common.utils.StringUtils;
import com.common.utils.uuid.IdUtils;
import com.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrganizationService {
    @Autowired
    SciencePersonMapper sciencePersonMapper;

    @Autowired
    OrganizationMapper organizationMapper;

    @Autowired
    ISysDeptService iSysDeptService;

    public CutPageBean<OrganizationBo> selectOrganizationList(OrganizationVo organizationVo) {
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String a=dateFormat.format(organizationVo.getLastDate());
//        if(a.equals("")){
//            organizationVo.setLastDate(null);
//        }
        organizationVo.setStart(organizationVo.getPage(),organizationVo.getPageSize());
        organizationVo.setIsLevel("0");
        CutPageBean<OrganizationBo> cutPage = new CutPageBean<>();
        List<OrganizationBo> list = organizationMapper.selectOrganizationList(organizationVo);
        List<String> id=list.stream().map(OrganizationBo::getId).collect(Collectors.toList());
        SciencePersonVo sciencePersonVo=new SciencePersonVo();
        for(int i = 0; i < list.size(); i++) {
            sciencePersonVo.setUnitId(id.get(i));
            List<SciencePersonBo> list1 = sciencePersonMapper.personCount(sciencePersonVo);
            List<String> count=list1.stream().map(SciencePersonBo::getCount).collect(Collectors.toList());
            list.get(i).setCount(count.get(0).toString());
        }
        int count = organizationMapper.selectOrganizationCount(organizationVo);
        cutPage.initCutPage(count,organizationVo.getPageSize(),list);
        return cutPage;
    }

    public  int insertOrganizationList(OrganizationBo organizationBo) {
        organizationBo.setIsLevel("0");
        organizationBo.setId(IdUtils.simpleUUID());
        boolean hasError = false;
        organizationBo.setIsExist(hasError);
        int row=organizationMapper.insert(organizationBo);
        SysDept sysDept=new SysDept();
        sysDept.setDeptName(organizationBo.getName());
        sysDept.setPhone(organizationBo.getTel());
        sysDept.setLeader(organizationBo.getAgerName());
        sysDept.setDeptRealId(organizationBo.getId());
        sysDept.setParentId(100L);
        iSysDeptService.insertDept(sysDept);
        return row;
    }

    public int updateOrganizationList(OrganizationBo organizationBo) {
        UpdateWrapper<OrganizationBo> uw = new UpdateWrapper<OrganizationBo>()
                .set(StringUtils.isEmpty(organizationBo.getOrderId()), "ORDER_ID", null)
                .eq("ID", organizationBo.getId());
        int row=organizationMapper.update(organizationBo,uw);
        SysDept sysDept=new SysDept();
        sysDept.setDeptName(organizationBo.getName());
        sysDept.setPhone(organizationBo.getTel());
        sysDept.setLeader(organizationBo.getAgerName());
        OrganizationVo organizationVo=new OrganizationVo();
        organizationVo.setDeptRealId(organizationBo.getId());
        List<SysDept> list = organizationMapper.sysDeptList(organizationVo);
        List<Long> deptId=list.stream().map(SysDept::getDeptId).collect(Collectors.toList());
        sysDept.setDeptId(deptId.get(0));
        iSysDeptService.updateDept(sysDept);
        return row;
    }

    public  int deletOrganizationList(OrganizationBo organizationBo) {
            String[] result = organizationBo.getIdList();
            int row =0;
            for (int i = 0; i < result.length; i++) {
                OrganizationVo organizationVo=new OrganizationVo();
                organizationVo.setDeptRealId(result[i]);
                List<SysDept> list = organizationMapper.sysDeptList(organizationVo);
                List<Long> deptId=list.stream().map(SysDept::getDeptId).collect(Collectors.toList());
                int row1=iSysDeptService.deleteDeptById(deptId.get(0));
                Map<String,Object> map=new HashMap<>();
                map.put("ID",result[i]);
                row=organizationMapper.deleteByMap(map);
            }
        return row;
    }

    public  List organizationListDetails(OrganizationVo organizationVo) {
        List<OrganizationBo> list = organizationMapper.organizationListDetails(organizationVo);
        return list;
    }

    public  int insertLevelOrganizationList(OrganizationBo organizationBo) {
        organizationBo.setIsLevel("1");
        boolean hasError = false;
        organizationBo.setIsExist(hasError);
        organizationBo.setId(IdUtils.simpleUUID());
        int row=organizationMapper.insert(organizationBo);
        OrganizationBo a =new OrganizationBo();
        boolean isReady = true;
        a.setIsExist(isReady);
        a.setId(organizationBo.getUpUnitId());
        a.setUpUnitId(null);
        a.setIsLevel("0");
        organizationMapper.update(a,new UpdateWrapper<OrganizationBo>().eq("ID",a.getId()));
        SysDept sysDept=new SysDept();
        sysDept.setDeptName(organizationBo.getName());
        sysDept.setPhone(organizationBo.getTel());
        sysDept.setLeader(organizationBo.getAgerName());
        sysDept.setDeptRealId(organizationBo.getId());
        OrganizationVo organizationVo=new OrganizationVo();
        organizationVo.setDeptRealId(organizationBo.getUpUnitId());
        List<SysDept> list = organizationMapper.sysDeptList(organizationVo);
        List<Long> deptId=list.stream().map(SysDept::getDeptId).collect(Collectors.toList());
        sysDept.setParentId(deptId.get(0));
        iSysDeptService.insertDept(sysDept);
        return row;
    }

    public  List levelOrganizationList(OrganizationVo organizationVo) {
        List<OrganizationBo> list = organizationMapper.levelOrganizationList(organizationVo);
        return list;
    }

    public  int deletLexelOrganizationList(OrganizationBo organizationBo) {
            OrganizationVo organizationVo=new OrganizationVo();
            organizationVo.setDeptRealId(organizationBo.getId());
            List<SysDept> list = organizationMapper.sysDeptList(organizationVo);
            List<Long> deptId=list.stream().map(SysDept::getDeptId).collect(Collectors.toList());
            iSysDeptService.deleteDeptById(deptId.get(0));
            Map<String,Object> map=new HashMap<>();
            map.put("ID",organizationBo.getId());
            int row=organizationMapper.deleteByMap(map);
            Integer a=organizationMapper.levelOrganizationCount(organizationBo);
            if(a==0){
                OrganizationBo b =new OrganizationBo();
                boolean hasError = false;
                b.setIsExist(hasError);
                b.setId(organizationBo.getUpUnitId());
                b.setUpUnitId(null);
                organizationMapper.update(b,new UpdateWrapper<OrganizationBo>().eq("ID",b.getId()));
            }
            return row;
        }

    public  List exportList(OrganizationVo organizationVo) {
        List<OrganizationBo> list = organizationMapper.exportList(organizationVo);
        return list;
    }
}
